package com.igeek.car.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.car.constant.MessageConstant;
import com.igeek.car.entity.Orders;
import com.igeek.car.service.OrdersService;
import com.igeek.car.result.Result;
import com.igeek.car.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/orders")
@Api(tags = "订单信息管理")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @ApiOperation("分页条件查询订单信息")
    @GetMapping("findPage")
    public Result findPage(PageQueryVO vo) {
        Page<Orders> page = ordersService.findPage(vo);
        if (page != null) {
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, page);
        }
        return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
    }

    @ApiOperation("新增订单")
    @PostMapping("add")
    public Result add(@RequestBody Orders order) {
        boolean flag = ordersService.save(order);
        if (flag) {
            return new Result(true, MessageConstant.ADD_ORDER_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_ORDER_FAIL);
    }

    @ApiOperation("通过ID查询订单信息")
    @GetMapping("{id}")
    public Result findOne(@PathVariable("id") Integer id) {
        Orders order = ordersService.getById(id);
        if (order != null) {
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, order);
        }
        return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
    }

    @ApiOperation("编辑订单信息")
    @PutMapping("update")
    public Result update(@RequestBody Orders order) {
        boolean flag = ordersService.updateById(order);
        if (flag) {
            return new Result(true, MessageConstant.UPDATE_ORDER_SUCCESS);
        }
        return new Result(false, MessageConstant.UPDATE_ORDER_FAIL);
    }

    @ApiOperation("删除订单信息")
    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Integer id) {
        boolean flag = ordersService.del(id);
        if (flag) {
            return new Result(true, MessageConstant.DELETE_ORDER_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_ORDER_FAIL);
    }

    @ApiOperation("生成订单号")
    @GetMapping("generate/orderNumber")
    public Result generateOrderNumber() {
        String orderNumber = ordersService.generateOrderNumber();
        if (orderNumber != null) {
            System.out.println("订单号生成成功: " + orderNumber);
            return new Result(true, MessageConstant.GENERATE_ORDER_NUMBER_SUCCESS, orderNumber);
        }
        System.out.println("订单号生成失败");
        return new Result(false, MessageConstant.GENERATE_ORDER_NUMBER_FAIL);
    }

    @ApiOperation("获取车次订单统计")
    @GetMapping("getOrderStatistics")
    public Result getOrderStatistics() {
        List<Map<String, Object>> statistics = ordersService.getOrderStatistics();
        if (statistics != null && !statistics.isEmpty()) {
            return new Result(true, MessageConstant.QUERY_ORDER_STATISTICS_SUCCESS, statistics);
        }
        return new Result(false, MessageConstant.QUERY_ORDER_STATISTICS_FAIL);
    }

    @ApiOperation("获取日期订单数量统计")
    @GetMapping("getDateOrderStatistics")
    public Result getDateOrderStatistics() {
        List<Map<String, Object>> dateStatistics = ordersService.getDateOrderStatistics();
        if (dateStatistics != null && !dateStatistics.isEmpty()) {
            return new Result(true, MessageConstant.QUERY_ORDER_STATISTICS_SUCCESS, dateStatistics);
        }
        return new Result(false, MessageConstant.QUERY_ORDER_STATISTICS_FAIL);
    }

    @ApiOperation("退票操作")
    @PutMapping("refund/{id}")
    public Result refund(@PathVariable("id") Integer id) {
        boolean flag = ordersService.refundOrder(id);
        if (flag) {
            return new Result(true, MessageConstant.REFUND_ORDER_SUCCESS);
        }
        return new Result(false, MessageConstant.REFUND_ORDER_FAIL);
    }

    @ApiOperation("订单改签")
    @PutMapping("reschedule/{id}")
    public Result reschedule(@PathVariable("id") Integer id, @RequestBody Orders orderDetails) {
        boolean flag = ordersService.rescheduleOrder(id, orderDetails);
        if (flag) {
            return new Result(true, "改签成功");
        }
        return new Result(false, "改签失败");
    }

    @ApiOperation("确认订单")
    @PutMapping("confirm/{id}")
    public Result confirmOrder(@PathVariable("id") Integer id) {
        boolean flag = ordersService.confirmOrder(id);
        if (flag) {
            return new Result(true, "订单确认成功");
        }
        return new Result(false, "订单确认失败");
    }
}